<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends Controller {

	public function _initialize()
    {
        //未登录、登录超时、长时间未动
    	if (empty($_SESSION['adminid']) || time() > $_SESSION['adminovertime'] || empty($_SESSION['adminovertime']) ) {

    		$this->redirect('/admin.php/Common/login');
    	}

        session('adminovertime',(time()+60*60*C('adminovertime')));

    }


    /**
     * [user 用户管理]
     * @return [type] [description]
     */
    public function user(){
    	$m_user = M("User");

        if (IS_POST) {
            $account = $_REQUEST['account'];
            $where['account'] = array('like','%'.$account.'%');
            $useridArr = $m_user->where($where)->order('id asc')->field('id')->select();
            $user_ids = array();
            foreach ($useridArr as $key => $value) {
                $user_ids[] = $value['id'];
            }

            $str_userids = implode(',', $user_ids);

            $where_user['id'] = array('in',$str_userids);
            $parameter = array('user_id'=>$str_userids);

            $count = $m_user->where($where_user)->count();
            $Page  = new \Think\Page($count,20,$parameter);
            $show  = $Page->adminshow();
        }elseif (IS_GET) {
            if (!empty($_REQUEST['user_id'])) {
                $where_user['id'] = array('in',$_REQUEST['user_id']);
                $parameter = array('user_id'=>$_REQUEST['user_id']);
            }
            $count = $m_user->where($where_user)->count();
            $Page  = new \Think\Page($count,20,$parameter);
            $show  = $Page->adminshow();
        
        }

        // $count = $m_user->count();
        // $Page  = new \Think\Page($count,20);
        // $show  = $Page->adminshow();

        $userList = $m_user->where($where_user)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('page',$show);
        $this->assign('userList',$userList);
    	$this->display();
    }


    /**
     * [collect 收款方式页面]
     * @return [type] [description]
     */
    public function collect(){
    	$id = $_REQUEST['id'];
    	$m_user = M('User');

    	$where_user['id'] = $id;
    	$userInfo = $m_user->where($where_user)->find();

    	$this->assign('userInfo',$userInfo);
    	$this->display();
    }


    /**
     * [ctrl_unlock 解封账号]
     * @return [type] [description]
     */
    public function ctrl_unlock(){
    	if(IS_AJAX){
    		$id = $_REQUEST['id'];
    		$m_user = M('User');

    		$where_user['id'] = $id;
    		$userInfo = $m_user->where($where_user)->find();

    		if (empty($userInfo['id'])) {
				$this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '目标账号不存在'
                ));
			}else{
				if ($userInfo['status'] == 1) {
	    			$this->ajaxReturn(array(
	                    'code'=>0,
	                    'msg' => '已解封，请勿重复操作'
	                ));
	    		}else{
	    			$save['status'] = 1;
	    			$res = $m_user->where($where_user)->save($save);
	    			if ($res) {
	    				$this->ajaxReturn(array(
		                    'code'=>1,
		                    'msg' => '操作成功'
		                ));
	    			}else{
	    				$this->ajaxReturn(array(
		                    'code'=>0,
		                    'msg' => '操作失败'
		                ));
	    			}
	    		}
			}
    	}else{
    		$this->redirect('/admin.php/Common/BadRequest');
    	}
    }


    /**
     * [ctrl_lock 冻结账号]
     * @return [type] [description]
     */
    public function ctrl_lock(){
    	if(IS_AJAX){
    		$id = $_REQUEST['id'];

    		$m_user = M('User');

    		$where_user['id'] = $id;
    		$userInfo = $m_user->where($where_user)->find();

    		if (empty($userInfo['id'])) {
				$this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '目标账号不存在'
                ));
			}else{
	    		if ($userInfo['status'] == 2) {
	    			$this->ajaxReturn(array(
	                    'code'=>0,
	                    'msg' => '已冻结，请勿重复操作'
	                ));
	    		}else{
	    			$save['status'] = 2;
	    			$res = $m_user->where($where_user)->save($save);
	    			if ($res) {
	    				$this->ajaxReturn(array(
		                    'code'=>1,
		                    'msg' => '操作成功'
		                ));
	    			}else{
	    				$this->ajaxReturn(array(
		                    'code'=>0,
		                    'msg' => '操作失败'
		                ));
	    			}
	    		}
			}
    	}else{
    		$this->redirect('/admin.php/Common/BadRequest');
    	}
    }
}